	#include "precomp.h"
	#include "Gun.h"

	int Gun::gun_types = 2;

	Gun::Gun(){}

	Gun::Gun(bool automatical, int damage, int max_bullets, int max_clips, float range, float shoot_interval) 
		: _automatical(automatical), _max_bullets(max_bullets), _max_clips(max_clips), _range(range), _shoot_interval(shoot_interval), _damage(damage)
	{
		_shooting = false;

		_bullets = _max_bullets;
		_clips = _max_clips;

		_last_shoot_time = 0;
	}

	void Gun::reload(){
		if(_clips > 0){
			int trans = (_max_bullets-_bullets > _clips ? _clips : _max_bullets-_bullets);
		
			_bullets += trans;

			_clips -= trans;
		}
	}

	float Gun::get_range(){
		return _range;
	}

	int Gun::get_damage(){
		return _damage;
	}

	int Gun::get_bullets(){
		return _bullets;
	}

	int Gun::get_clips(){
		return _clips;
	}

	bool Gun::is_shooting(){
		return _shooting;
	}

	void Gun::set_shooting(bool shooting){
		_shooting = shooting;
	}

	bool Gun::shoot(){
		if((!_automatical && _shooting) || (_automatical && CL_System::get_time() - _last_shoot_time < _shoot_interval)){
			return false;
		}

		if(_bullets > 0){
			_shooting = true;

			_last_shoot_time = CL_System::get_time();

			_bullets--;
			return true;
		}
		return false;
	}